package com.future.mayi.fragment;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.future.baselib.entity.BaseResponse;
import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.baselib.utils.PatternUtils;
import com.future.baselib.utils.ToastUtil;
import com.future.baselib.utils.ToastUtils;
import com.future.mayi.R;
import com.future.mayi.activity.BaseFragment;
import com.future.mayi.model.interfaces.SettingPayPasswordListener;
import com.future.mayi.utils.SharedPreferencesUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class VerifyPhoneFragment extends BaseFragment {

    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R.id.edt_code)
    EditText edtCode;
    @BindView(R.id.tv_enter)
    TextView tvEnter;

    private SettingPayPasswordListener listener;
    private String mobile, countryId, countryCode;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_verify_phone;
    }

    @Override
    protected void initView() {
        mobile = (String) SharedPreferencesUtils.get(getActivity(), SharedPreferencesUtils.SpEnum.LoginPhone, "");
        countryId = (String) SharedPreferencesUtils.get(getActivity(), SharedPreferencesUtils.SpEnum.LoginPhoneCountryId, "");
        if (TextUtils.isEmpty(countryId)) countryId = null;
        countryCode = (String) SharedPreferencesUtils.get(getActivity(), SharedPreferencesUtils.SpEnum.LoginPhoneCountryCode, "");
        if (TextUtils.isEmpty(countryCode)) countryCode = null;
        tvPhone.setText(mobile == null ? "" : ((TextUtils.isEmpty(countryCode) ? "+86" : ("+" + countryCode)) + mobile));
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }


    public void setListener(SettingPayPasswordListener listener) {
        this.listener = listener;
    }


    @OnClick({R.id.tv_get_code, R.id.tv_enter})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_get_code:
                getCode();
                break;
            case R.id.tv_enter:
                if (TextUtils.isEmpty(edtCode.getText())) {
                    ToastUtil.getInstance().show(getActivity(), "请输入验证码");
                    return;
                }
                String codes = edtCode.getText().toString();
                if (codes.length() < 4) {
                    ToastUtil.getInstance().show(getActivity(), "请输入正确的验证码");
                    return;
                }
                if (listener != null)
                    listener.edtCode(mobile, codes);
                break;
        }
    }


    @SuppressWarnings("unchecked")
    private void getCode() {

        tvGetCode.setEnabled(false);
        new HttpRequest<DefaultResponse>()
                .with(getActivity())
                .setApiCode(ApiCst.sendCode)
                .addParam("is_new", 1)
                .addParam("mobile", mobile)
                .addParam("country_id", countryId)
                .addParam("event", "setpaypwd")
                .setListener(new HttpRequest.OnNetworkListener() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        toast(response.message);
                        isRun = true;
                        new Thread(runnable).start();
                    }

                    @Override
                    public void onFail(String message, BaseResponse response) {
                        tvGetCode.setEnabled(true);
                        toast(message);
                    }
                }).start(new DefaultResponse());

    }


    private boolean isRun = false;

    //验证码发送倒计时
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tvGetCode.setEnabled(false);
                }
            });
            for (int i = 60; i > 0; i--) {
                final int finalI = i;
                if (!isRun) return;
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvGetCode.setText("重新获取(" + finalI + ")");
                    }
                });
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            if (!isRun) return;
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tvGetCode.setEnabled(true);
                    tvGetCode.setText("重新获取");
                }
            });
            isRun = false;
        }
    };


    @Override
    public void onDestroy() {
        super.onDestroy();
        isRun = false;
    }
}
